﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Game.Main.View;

public class TouchButton : MonoBehaviour
{
    private bool isPressed = false;

    void Update()
    {
        // 处理触摸输入
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0); // 获取第一个触摸点

            switch (touch.phase)
            {
                case TouchPhase.Began:
                    OnTouchDown(touch.position);
                    break;
                case TouchPhase.Ended:
                    OnTouchUp();
                    break;
            }
        }
    }

    private void OnTouchDown(Vector2 touchPosition)
    {
        // 将触摸位置从屏幕坐标转换为世界坐标
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(touchPosition);

        // 使用射线检测判断是否点击了当前对象的碰撞体
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);

        if (hit.collider != null && hit.collider.gameObject == gameObject)
        {
            isPressed = true;
            Debug.Log("Button Pressed!");
            OnButtonPressed(); // 触发按钮按下逻辑
        }
    }

    private void OnTouchUp()
    {
        if (isPressed)
        {
            isPressed = false;
            Debug.Log("Button Released!");
            OnButtonReleased(); // 触发按钮释放逻辑
        }
    }

    // 按钮按下时的逻辑
    private void OnButtonPressed()
    {
        // 例如：播放按下音效、改变颜色等
    }

    // 按钮释放时的逻辑
    private void OnButtonReleased()
    {
        //AdManager.Instance.ShowVideo(() =>
        //{

        //});
        AdManager.Instance.ShowVideo(() =>
        {
            SBislock();
        });
    }
    public void SBislock()
    {
        StorageBox.Instance.IsLock(false);
    }
}
